package com.aitou.mrylive.presentation.ui.main.index;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jakewharton.rxbinding.view.RxView;
import com.aitou.mrylive.R;
import com.aitou.mrylive.data.bean.HotAnchorSummary;
import com.aitou.mrylive.data.repository.SourceFactory;
import com.aitou.mrylive.presentation.ui.base.BaseFragment;
import com.aitou.mrylive.presentation.ui.base.ptr.BasePtr;
import com.aitou.mrylive.presentation.ui.base.recycler.SimpleRecyclerAdapter;
import com.aitou.mrylive.presentation.ui.base.recycler.SimpleRecyclerHolder;
import com.aitou.mrylive.presentation.ui.main.IndexFragment;
import com.aitou.mrylive.presentation.ui.room.RoomActivity;
import com.aitou.mrylive.presentation.ui.room.player.PlayerFragment;
import com.aitou.mrylive.util.Const;
import com.aitou.mrylive.util.L;
import com.aitou.mrylive.util.Spans;
import com.umeng.analytics.MobclickAgent;

import java.util.List;
import java.util.concurrent.TimeUnit;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import rx.functions.Action1;

/**
 * 关注好友的直播列表页。
 *
 * @author Muyangmin
 * @since 1.0.0
 */
public class FollowedAnchorLiveFragment extends BaseFragment implements FollowedAnchorInterface {

    private PtrFrameLayout ptrFrameLayout;
    private RelativeLayout rlEmptyLive;
    private RecyclerView recyclerView;
    private FollowedAnchorAdapter adapter;
    private FollowedAnchorPresenter presenter;
//    跳转到热门
    private TextView goHot;
//    跳转到热门的回调接口
    private FollwedShowHot follwedShowHot;
//    private CircleProgress mProgressView;

    public static FollowedAnchorLiveFragment newInstance() {
        return new FollowedAnchorLiveFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.frag_followed_anchor_live;
    }

    @Override
    public void onResume() {
//        MobclickAgent.onPageStart("主页-关注");
        super.onResume();
    }

    @Override
    public void onPause() {
//        MobclickAgent.onPageStart("主页-最新");
        super.onPause();
    }

    @Override
    protected void initViews(View view) {

//        mProgressView = $(view,R.id.progress);
//        mProgressView.startAnim();

        presenter = new FollowedAnchorPresenter(this);
        ptrFrameLayout = $(view, R.id.followed_anchor_ptr);
        BasePtr.setRefreshOnlyStyle(ptrFrameLayout);
        goHot=$(view,R.id.followed_anchor_tv_view_hot);
        recyclerView = $(view, R.id.followed_anchor_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        ptrFrameLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return super.checkCanDoRefresh(frame, recyclerView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.loadFirstPage();
            }
        });

        rlEmptyLive = $(view, R.id.followed_anchor_rl_no_live);
        RxView.clicks(rlEmptyLive)
                .throttleFirst(Const.VIEW_THROTTLE_TIME, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Fragment parentFragment = getParentFragment();
                        if (parentFragment == null || (!(parentFragment instanceof IndexFragment)
                        )) {
                            L.e(LOG_TAG, "Parent fragment unexpected, is %s!", parentFragment);
                            return;
                        }
                        ((IndexFragment) parentFragment).switchToHotList();
                    }
                });
        goHot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("mrl","点击里");
                follwedShowHot.showHot();
            }
        });
        ptrFrameLayout.autoRefresh();
    }


    public interface FollwedShowHot{
        void showHot();
    }

    public void setFollwedShowHot(FollwedShowHot follwedShowHot) {
        this.follwedShowHot = follwedShowHot;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.unsubscribeTasks();
    }

    @Override
    public void showLoadingComplete() {
//        super.showLoadingComplete();
        ptrFrameLayout.refreshComplete();
    }

    @Override
    public void showData(List<HotAnchorSummary> list) {
        rlEmptyLive.setVisibility((list == null || list.isEmpty())? View.VISIBLE : View.INVISIBLE);
        if (adapter == null) {
            adapter = new FollowedAnchorAdapter(list);
            recyclerView.setAdapter(adapter);
        } else {
            adapter.setDataList(list);
        }
    }

    @Override
    public void appendData(List<HotAnchorSummary> list) {
        adapter.appendData(list);
    }

    private class FollowedAnchorAdapter extends SimpleRecyclerAdapter<HotAnchorSummary,
            FollowedAnchorHolder> {
        public FollowedAnchorAdapter(List<HotAnchorSummary> hotAnchorSummaries) {
            super(hotAnchorSummaries);
        }

        @Override
        protected int getItemLayoutId(int viewType) {
            return R.layout.item_hot_anchor;
        }

        @NonNull
        @Override
        protected FollowedAnchorHolder createHolder(View view) {
            return new FollowedAnchorHolder(view);
        }
    }

    private class FollowedAnchorHolder extends SimpleRecyclerHolder<HotAnchorSummary> {

        private SimpleDraweeView drawSnap;
        private TextView tvNickname;
        private TextView tvLocation;
        private TextView tvOnlineCount;
        private TextView note;
        private SimpleDraweeView drawAvatar;

        @SuppressWarnings("unused")
        public FollowedAnchorHolder(View itemView) {
            super(itemView);
            drawAvatar = (SimpleDraweeView) itemView.findViewById(R.id.img_user_avatar);
            drawSnap = (SimpleDraweeView) itemView.findViewById(R.id
                    .item_hot_anchor_img_front_cover);
            tvNickname = (TextView) itemView.findViewById(R.id.item_hot_anchor_tv_nickname);
            tvLocation = (TextView) itemView.findViewById(R.id.item_hot_anchor_tv_location);
            tvOnlineCount = (TextView) itemView.findViewById(R.id.item_hot_anchor_tv_online_count);
        }

        @Override
        public void displayData(final HotAnchorSummary data) {
            drawAvatar.setImageURI(SourceFactory.wrapPathToUri(data.getAvatar()));
            drawSnap.setImageURI(SourceFactory.wrapPathToUri(data.getSnap()));
            tvNickname.setText(data.getNickname());
            tvLocation.setText(data.getCity());
            tvOnlineCount.setText(Spans.createSpan("", String.valueOf(data.getOnlineCount()),
                    itemView.getContext().getString(R.string.hot_anchor_online_count_suffix), new
                            ForegroundColorSpan(ContextCompat.getColor(itemView.getContext(), R.color.yunkacolor)),
                    new RelativeSizeSpan(1.6F)));
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(RoomActivity.createIntent(getActivity(),
                            RoomActivity.TYPE_VIEW_LIVE,
                            data.getCurrentRoomNum(),
                            data.getId(),
                            PlayerFragment.createArgs(data)));
                    getActivity().overridePendingTransition(R.anim.fragment_slide_left_in, R.anim
                            .fragment_slide_left_out);
                }
            });
            //TODO note
//            if (StreamingProfile.StreamStatus.class.isAnnotation()) {
//                mProgressView.startAnim();
//            }
//            mProgressView.stopAnim();
        }
    }
}
